# -*- coding: utf-8 -*-
import requests
import csv
import time

# 抓取海报
url = 'https://api.douban.com/v2/movie/1292052?apikey=0df993c66c0c636e29ecbb5344252a4a'
req = requests.get(url, headers={'user-agent': 'chrome'})
# print(req)
dic_req = req.json()
# print(dic_req)
ima = dic_req.get('image')
# print(ima)
hai_bao = requests.get(ima)
# print(hai_bao.content)

with open('图片/肖申克的救赎.jpg', 'wb') as f:
    f.write(hai_bao.content)

# 去除中括号
def lie_b():
    l = ' '.join(lst)


# 批量抓取电影数据
def zhua_qu(dic_req, ye_shu, lst):
    '''每次抓取是20个电影数据，需要循环20次抓取数据，然后存入列表'''
    for i in range(20):
        xu_h = i + 1 + ye_shu
        if xu_h > 250:
            break
        print(f'{xu_h}=====', end='')
        # 截取总数据段
        zsj = dic_req['subjects'][i]
        # 电影ID
        id = '电影ID：%s' % zsj.get('id')
        # print(id)
        # 电影名
        dym = zsj.get('title')
        dian_y = f'电影：{dym}'
        print(dym)
        # 电影类型
        lei_x = ' '.join(zsj.get('genres'))
        lei_x = f'类型：{lei_x}'
        # print(lei_x)
        # 评分
        ping_f = '评分：%s' % zsj['rating']['average']
        # print(ping_f)
        # 导演
        dy = zsj['directors'][0]
        dao_y = '导演：%s' % dy.get('name')
        # print(dao_y)
        # 主演(因为主演不是一个，所以同样需要循环抓取
        try:
            zy = ['主演：']
            for x in range(3):
                yy = zsj['casts'][x]
                zhu_y = yy.get('name')
                zy.append(zhu_y)
        except:
            zy = ['无']
            # print('无')
        zy = ' '.join(zy)
        # print(zy)
        # 时长
        shi_c = zsj.get('durations')
        shi_c = f'时长：{shi_c[0]}'
        # print(shi_c)
        # 海报
        hb = zsj['images']['large']
        # print(f'海报：{hb}')
        hb1 = requests.get(hb)
        with open(f'图片/{dym}.jpg', 'wb') as f:
            f.write(hb1.content)
        # 简介
        jian_j = '简介地址：%s' % zsj.get('alt')
        # print(f'{jian_j}\n')
        # id，电影名，类型，评分，导演，主演，时长，简介
        l = [xu_h, id, dian_y, lei_x, ping_f, dao_y, zy, shi_c, jian_j]
        lst.append(l)
def huou_qu():
    lst = []
    for i in range(0, 250, 20):
        print(i)
        url = f'https://api.douban.com/v2/movie/top250?start={i}&apikey=0df993c66c0c636e29ecbb5344252a4a'
        try:
            req = requests.get(url, headers={'user-agent': 'chrome'})
        except:
            print('获取失败')
        dic_req = req.json()
        # print(dic_req)
        zhua_qu(dic_req, i, lst)
        time.sleep(1)
    return lst

# 写入csv
# id，电影名，类型，评分，导演，主演，时长，简介
lst = huou_qu()
# print(lst)
headers = ['0', 'ID', '电影名', '类型', '评分', '导演', '主演', '时长', '简介地址']
with open('dbdyT250.csv', 'w', encoding='utf8') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    f_csv.writerows(lst)